package com.teampower.idea.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.teampower.idea.dao.UsersDAO;
import com.teampower.idea.pojo.Users;
import com.teampower.idea.util.ToolsUtil;

public class UsersDAOImpl extends HibernateDaoSupport implements UsersDAO {
	
	//加载单个用户,登录验证
	public Users load(String username,String password) {
		String hql = "from Users as t where t.username=? and t.password=?";
		List list = null;
		list = this.getHibernateTemplate().find(hql,new Object[]{username,password});
		if(list.size()==1){
			return (Users) list.get(0);
		}else{
			return null;
		}
	}
	
	//加载单个用户,注册验证
	public Users load(String username) {
		String hql = "from Users as t where t.username=?";
		List list = null;
		list = this.getHibernateTemplate().find(hql,username);
		if(list.size()==1){
			return (Users) list.get(0);
		}else{
			return null;
		}
	}
	
	//用户注册
	public void save(Users users) {
		this.getHibernateTemplate().save(users);
	}
	
	//查询总记录数
	public int getCounts() {
		String hql = "select count(id) from Users as t where permission!='9'";
		int i = 0;
		List list = null;
		list = this.getHibernateTemplate().find(hql);
		if(list.size()==1){
			i = ToolsUtil.getInt(list.get(0).toString());
		}
		return i;
	}
	
	//分页查询
	public List getAll(int startIndex,int pageSize) {
		Session session = SessionFactoryUtils.getSession(this.getHibernateTemplate().getSessionFactory(),true);
		Query query = session.createQuery("from Users as t where permission!='9' order by inputdatetime desc");
		query.setFirstResult(startIndex);
		query.setMaxResults(pageSize);
		List list = query.list();
		SessionFactoryUtils.releaseSession(session,this.getHibernateTemplate().getSessionFactory());
		return list;
	}
	
}

